#ifndef __KDTREE_NODE_H__
#define __KDTREE_NODE_H__

#include "kdtree_prerequisites.h"

namespace fmrt
{
	/** This class represents a node in the kd-tree. 
		A kd-tree is a special form of BSP tree, all of the splitting planes in kd-tree are along to one 
		of the three axis. 
		
	*/ 
	class KDTreeNode : public Ogre::NodeAlloc
	{
	public: 
		
		void initLeaf(Ogre::uint32 *primNums, int nPrimitives);
		void initInterior(Ogre::uint32 axis, uint32_t ac, float s) {
			split = s;
			flags = axis;
			aboveChild |= (ac << 2);
		}
		float getSplitPos() const				{ return split; }
		Ogre::uint32 getNumPrimitives() const	{ return nPrimitives >> 2; }
		Ogre::uint32 getSplitAxis() const		{ return flags & 3; }
		bool isLeaf() const						{ return (flags & 3) == 3; }
		Ogre::uint32 getAboveNode() const		{ return aboveChild >> 2; }
		
		union {
			float split;				// Interior
			Ogre::uint32 onePrimitive;  // Leaf
			Ogre::uint32 *primitives;   // Leaf
		};

	private:
		union {
			Ogre::uint32 flags;         // Both
			Ogre::uint32 nPrimitives;   // Leaf
			Ogre::uint32 aboveChild;    // Interior
		};
	}; 
}


#endif